package com.shop.dao.impl;

import com.shop.dao.StoreDao;
import com.shop.entity.Store;
import com.shop.exception.DAOException;
import com.shop.transaction.TransactionManager;
import com.shop.util.ConnectionUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StoreDaoImpl implements StoreDao {
    @Override
    public Store gteStoreById(int sid) throws DAOException {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = TransactionManager.getTransactionConn();
            pstmt = conn.prepareStatement("SELECT s.`s_id`,s.`s_name`,s.`s_pwd`,s.`s_storename`,s.`u_id`FROM store s WHERE s_id=?");
            pstmt.setInt(1,sid);
            rs = pstmt.executeQuery();
            if(rs.next()){
                int s_id = rs.getInt("s_id");
                String s_name = rs.getString("s_name");
                String s_pwd = rs.getString("s_pwd");
                String s_storename = rs.getString("s_storename");
                int u_id = rs.getInt("u_id");
                return new Store(s_id,s_name,s_pwd,s_storename,u_id);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new DAOException(throwables);
        }finally {
            ConnectionUtils.close(rs,pstmt,null);
        }
        return null;
    }
}
